Learning by doing

Doing before understanding

Sticky notes

I’m done with the exercise, please come and check my work.
I’ve encountered a problem and I need help.
Something you learned or think will be useful
Something that we covered too quickly, too slowly, or that you think is irrelevant

Setup your local working directory

Do this once for each computer you work on


Tell git who you are

$ git config --global user.name "John Doe"
$ git config --global user.email "johndoe@example.com"
$ git credential-osxkeychain
$ git config --global credential.helper osxkeychain

Configure RStudio


Create a new repository on GitHub

Do this once for each project you work on


Get the URL for your new repository


Create a new RStudio project


Create a new R Markdown file

Do this one or more times for each project you work on


Save your new R Markdown file


Tell git to keep track of your new file(s)


Commit your changes

[master (root-commit) 2757d93] Initial add
 3 files changed, 47 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 my_first_markdown.Rmd
 create mode 100644 session_1.Rproj

Push your changes to GitHub

error: unable to read askpass response from 'rpostback-askpass'
fatal: could not read Username for 'https://github.com': Device not configured
$ git push
To https://github.com/jasonmtroos/session_1
   f5d8fc0..dbea7bb  master -> master

Compile your R Markdown file

Make a change to your R Markdown file

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

This is my first markdown file.

## R Markdown

This is an R Markdown document. Markdown...

Commit your changes and push them to GitHub

Do this whenever you make changes to your file you want to keep

View your file’s history

Do this when trying to figure out where and when you introduced a mistake into your file


View your remote repository

What you’ve done so far

  1. Set up your workspace
  2. Created a git repository on GitHub and cloned it into your local workspace
  3. Created an RStudio project and added an R Markdown file to it
  4. Compiled your R Markdown file into an HTML document
  5. Committed your edits to git and pushed them to GitHub
  6. Viewed your commit history

What’s next

Installing packages

library("tidyverse")
install.packages("tidyverse", dependencies = TRUE)

Edit your R Markdown file

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

[Deleted from this point forward]

Download data

Download sleep study data
```{r}
library(readr)
sleep <- read_csv(
    "https://vincentarelbundock.github.io/Rdatasets/csv/lme4/sleepstudy.csv")
head(sleep)
```

Commit your changes

Committing and pushing

Plot the data

Plot the data by subject
```{r}
library(ggplot2)
ggplot(sleep, aes(x = Days, y = Reaction)) +
  geom_point() +
  geom_smooth() +
  scale_x_continuous(breaks = seq(0, 8, by = 2)) +
  facet_wrap(~ Subject)
```

Group and summarise

Summary statistics by subject

```{r}
library(dplyr)
sleep %>%
  group_by(Subject) %>%
  summarise(mean(Reaction))
```

Mean-center reaction times and plot on a single axis

```{r}
sleep %>%
  group_by(Subject) %>%
  mutate(mean_centered_reaction = Reaction - mean(Reaction)) %>%
  ggplot(aes(x = Days, y = mean_centered_reaction,
      colour = factor(Subject))) + 
    geom_smooth(show.legend = FALSE, se = FALSE) +
    scale_x_continuous(breaks = seq(0, 8, by = 2))
```

Commit your changes and push them to GitHub

Why are we doing all of this?

  1. Workspace organization and git
  2. Automation and reproducibility via R Markdown

Because without these practices, you cannot expect to successfully manage work that involves large and/or disparate data sets

Workspace ideas

Reproducibility and automation ideas

More about git

More about R Markdown

5 * 10 + 2
## [1] 52

Like so.

R Markdown example

This is the code I used to generate the preceding slide.

More about R Markdown
=====================
* Based on the idea of *literate programming*
* Simple rules for formatting text as HTML (or PDF via latex)
* Code can be inserted inline with other text using special
  delimiters
* When compiling the markdown file, the code is executed and
  results are inserted into the final HTML file

```{r}
5 * 10 + 2
```

Like so.

More about R Markdown

What you can do during the lunch break

  1. Ensure your R environment is in order (see pre-work assignment on Canvas)
  2. Get familiar with R Markdown (mandatory) and git (optional) – see previous slides for links
  3. Follow the R tutorials I developed for my Marketing Analytics MSc course
    • Assignment 1 – learning R (mandatory if you are lost right now, otherwise optional)
    • Assignment 2, sections 1–2 – linear and logistic regression (mandatory if you don’t know what y ~ x means in R, otherwise optional)
    • Assignment 3, section 2 – ggplot2 (mandatory)
    • Assignment 4 – more ggplot2 and dplyr (mandatory)